﻿using System;
using System.Collections.Generic;
using System.Text;
using net.veekee.common;
using net.veekee.common.data;

namespace net.veekee.logprocessor.plugin.input
{
    public abstract class AParser : IDisposable
    {
        private static AParser Instance = null;
        public static AParser GetInstance() { return AParser.Instance; }
        public static void SetInstance(AParser _instance)
        {
            bool keepExistingEntries = ((AParser.Instance != null) && Options.GetInstance().IsEnabled("buffer.keepalive"));
            if (keepExistingEntries)
            {
                AParser.Instance.ReinitStats();
            }
            else
            {
                if (AParser.Instance != null)
                    AParser.Instance.Dispose();
                AParser.Instance = _instance;
            }
        }

        public List<FileEntry> entries = null;
        private int maxLevel = 99;
        public int MaxAllowedLevel
        {
            get
            {
                return this.maxLevel;
            }
        }

        public AParser()
        {
            if (Options.GetInstance().Get("maxlevel") != null)
                Int32.TryParse(Options.GetInstance().Get("maxlevel"), out this.maxLevel);
            this.entries = new List<FileEntry>();
        }

        protected virtual void ReinitStats()
        {
        }

        public virtual bool SupportsEntry(String filename)
        {
            if (!System.IO.File.Exists(filename))
            {
                Log.GetInstance().Error("{0} is not supported. File couln't be found.", filename);
                return false;
            }
            return true;
        }


        public virtual bool Parse(String archive, String filename, bool isFirstFile, bool isLastFile, int fileID, int fileCount)
        {
            if (! this.SupportsEntry(filename))
            {
                return false;
            }
            Log.GetInstance().Debug("Processing {0} ({1}/{2})", filename, fileID, fileCount);
            return true;
        }

        public virtual void Dispose()
        {
            this.entries.Clear();
        }
    }
}
